package com.hzdl.hotel.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.hotel.bean.Check;
import com.hzdl.hotel.bean.Customer;
import com.hzdl.hotel.bean.GuestRoom;
import com.hzdl.hotel.service.RoomService;
import com.hzdl.hotel.util.AJAXResult;
import com.hzdl.hotel.vo.CheckVo;
import com.hzdl.hotel.vo.GuestRoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private RoomService rs;

    @RequestMapping("/roomInit")
    private String roomInit(){
        return "administer/guestroom/roomManage";
    }
    @RequestMapping("/roomShow")
    @ResponseBody
    public AJAXResult roomShow(GuestRoomVo grv){
        if (grv.getPage() == null) {
            grv.setPage(1);

        }
        if (grv.getLimit() == null) {
            grv.setLimit(5);
        }
        PageInfo pageInfo = rs.findRoomByWhere(grv);
        return new AJAXResult(0, "", pageInfo.getList(), pageInfo.getTotal());
    }

    @RequestMapping("/uploadImg")
    @ResponseBody
    public AJAXResult uploadImg(@RequestParam("f1") MultipartFile file) {
        // 在IE11/Edge浏览器下面，获取到的路径信息带有盘符
        //文件名称
        try {
            String filename = file.getOriginalFilename();
            // 文件上传时，Chrome和IE/Edge对于originalFilename处理不同
            // Chrome 会获取到该文件的直接文件名称，IE/Edge会获取到文件上传时完整路径/文件名
            // Check for Unix-style path
            int unixSep = filename.lastIndexOf('/');
            // Check for Windows-style path
            int winSep = filename.lastIndexOf('\\');
            int pos = (winSep > unixSep ? winSep : unixSep);
            if (pos != -1) {
                filename = filename.substring(pos + 1);
            }

            String path = ResourceUtils.getURL("classpath:").getPath() + "static/img/room/" + filename;

            file.transferTo(new File(path));
            Map<String,Object> map=new HashMap<>();
            map.put("src","/static/img/room/"+filename);
            return new AJAXResult(map);
        }
        catch(Exception e){
            return new AJAXResult(2,e.getMessage());
        }
    }

    @RequestMapping("/addRoom")
    @ResponseBody
    public AJAXResult addCar(GuestRoomVo guestRoomVo) {
        int row = rs.insertRoom(guestRoomVo);
        if (row == 0) {
            return AJAXResult.ADD_ERROR;
        }
        return AJAXResult.ADD_SUCCESS;
    }

    @RequestMapping("/updateRoom")
    @ResponseBody
    public AJAXResult updateCar(GuestRoomVo guestRoomVo) {
        int row = rs.updateRoom(guestRoomVo);
        if (row == 0) {
            return AJAXResult.UPDATE_ERROR;
        }
        return AJAXResult.UPDATE_SUCCESS;
    }

    @RequestMapping("/roomDelete")
    @ResponseBody
    public AJAXResult carDelete(Integer hId) {
        int row = rs.deleteRoom(hId);
        if (row == 0) {
            return AJAXResult.DELETE_ERROR;
        }
        return AJAXResult.DELETE_SUCCESS;
    }

    @RequestMapping("/deleteAllRoom")
    @ResponseBody
    public AJAXResult deleteAllCar(String ids) {
        String[] hId = ids.split(",");
        int row = 0;
        for (int i = 0; i < hId.length; i++) {
            row += rs.deleteRoom(Integer.valueOf(hId[i]));
        }
        if (row == 0) {
            return AJAXResult.DELETE_ERROR;
        }
        return AJAXResult.DELETE_SUCCESS;
    }
    @RequestMapping("/changeRoomInit")
    public String changeRoomInit(){
        return "administer/guestroom/rentRoom";
    }

    @RequestMapping("/generateInfo")
    @ResponseBody
    public AJAXResult generateInfo(CheckVo checkVo,HttpSession session){
        Map<String,Object> map = new HashMap<>();
        String checkId="SH_";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        checkId+=sdf.format(new Date());
        checkId+="_";
        Random r = new Random();
        for(int i=0;i<6;i++){
            checkId+=r.nextInt(10);
        }
        checkId+="_";
        for(int i=0;i<3;i++){
            checkId+=r.nextInt(10);
        }
        map.put("checkId",checkId);
        Object o = session.getAttribute("check");
        if(o!=null){
            Check check=(Check)o;
            map.put("hId",check.gethId());
            map.put("phone",check.getPhone());
        }

        return new AJAXResult(1,"",map);
    }

    @RequestMapping("/returnRoomInit")
    public String returnRoomInit(){
        return "administer/guestroom/roomReturn";
    }

    @RequestMapping("/infoCheck")
    @ResponseBody
    public AJAXResult infoCheck(CheckVo checkVo, HttpSession session){
        Check c =rs.findCustomer2(checkVo);
         if(c!=null){
             session.setAttribute("check",c);
             return new AJAXResult(1);
         }else{
             return new AJAXResult(0,"该客户未预订过房间");
         }
    }

    @RequestMapping("/changeRoom")
    @ResponseBody
    public AJAXResult changeRoom(CheckVo checkVo){
       int row=rs.changeRoom(checkVo);
       if(row!=0){
           return new AJAXResult(1,"更换成功");
       }else{
           return new AJAXResult(0,"更换失败");
       }
    }

    @RequestMapping("/checkCustomerIfRent")
    @ResponseBody
    public AJAXResult checkCustomerIfRent(CheckVo checkCo){
    List<Check> c =rs.findCustomer(checkCo.getLoginName());
    if(c!=null){
        return new AJAXResult(1,"",c);
    }else{
        return new AJAXResult(0,"该客户暂无需要退订的房间");
    }
    }

    @RequestMapping("/roomRelativeInfo")
    @ResponseBody
    public AJAXResult roomReturn(CheckVo checkVo){
        Map<String,Object> map = new HashMap<>();
        Check c =rs.findCustomer1(checkVo);
        map.put("check",c);
        GuestRoom room = rs.findRoomByHId(c.gethId());
        map.put("room",room);
        Customer customer=rs.findCustomerByLoginName(checkVo.getLoginName());
        map.put("customer",customer);
        return new AJAXResult(1,map);
    }
     @RequestMapping("/returnRoom")
    @ResponseBody
    public AJAXResult returnRoom(CheckVo checkVo){
        int row=rs.returnRoom(checkVo);
        if(row!=0){
            return new AJAXResult(1,"退房成功");
        }else{
            return new AJAXResult(0,"退房失败");
        }
     }


}
